package homewebview;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.yztc.thinkpad.mandemo.R;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/8.
 */
public class VpWebView extends AppCompatActivity {
    private  WebView homeVpWv;
    private   String mUrl;
    private   TextView wv_tv;
    private ImageView wvIvBack;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_vp_wv);
        initData();
        initWv();
        wvIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        ButterKnife.bind(this);
    }
    private void initData() {
        wv_tv= (TextView) findViewById(R.id.wv_tv);
        homeVpWv= (WebView) findViewById(R.id.home_vp_wv);
        wvIvBack= (ImageView) findViewById(R.id.wv_iv_back);
        Intent intent = getIntent();
        mUrl= intent.getStringExtra("url");
        String theme_name = intent.getStringExtra("theme_name");
        wv_tv.setText(theme_name);
    }
    private void initWv() {
        homeVpWv.getSettings().setDefaultTextEncodingName("utf-8");
        homeVpWv.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
        //允许js执行 如果访问的html页面中存在JavaScript脚本语言  webview中需要设置支持
        homeVpWv.getSettings().setJavaScriptEnabled(true);
        //WebChromeClient 主要处理解析 渲染网页等浏览器操作的事情
        homeVpWv.setWebChromeClient(new WebChromeClient());
        //点击网页中的超链接时不弹出浏览器的窗口 在webview控件中加载url
        homeVpWv.setWebViewClient(new WebViewClient());
        // loadUrl()该方法直接加载网络地址
        homeVpWv.loadUrl(mUrl);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            if(homeVpWv.canGoBack()){
                homeVpWv.goBack();//goBack() 表示返回webview中的上一页面
            }else{
                finish();
            }
        }
        return true;
    }
}
